<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <h2>{{ title }}</h2>
        <input type="text" v-model="title" @keydown.enter="addTodo">
        <button v-if="active < all" @click="clear">清理</button>
        <ul v-if="todos.length">
            <li v-for="todo in todos">
                <input type="checkbox" v-model="todo.done">
                <span :class="{ done: todo.done }">{{ todo.title }}</span>
            </li>
        </ul>
        <div v-else>暂时没有数据</div>
        <div>
            全选<input type="checkbox" v-model="allDone">
            {{ active }} / {{ all }}
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.4.38/vue.global.js"></script>
    <script>
        const App = {
            data() {
                return {
                    title: 'hello world',
                    todos: [
                        { title: '学习', done: false },
                        { title: '写代码', done: true }
                    ]
                }
            },
            methods: {
                addTodo() {
                    this.todos.push({
                        title: this.title,
                        done: false
                    })
                    this.title = ''
                },
                clear() {
                    this.todos = this.todos.filter(todo => !todo.done)
                }
            },
            computed: {
                active() {
                    return this.todos.filter(todo => !todo.done).length
                },
                all() {
                    return this.todos.length
                },
                allDone: {
                    get: function() {
                        return this.active === 0
                    },
                    set: function(value) {
                        this.todos.forEach(todo => todo.done = value);
                    }
                }
            }
        }
        // 启动应用
        Vue.createApp(App).mount('#app')
    </script>

    <style>
        .done {
            color: gray;
            text-decoration: line-through;
        }
    </style>

</html>